package com.qf.day04.homework;

import java.util.Scanner;

/*
(选做)死亡游戏(约瑟夫环问题)。8个死刑犯围成一圈(序号0-7)，依次报数，每次报到三，该死刑犯7出局。问最后存活的是几号。
 */
public class Homework2 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("请输入玩的人数");
            int num=scanner.nextInt();
        System.out.println("存活下来人的下标是:"+killGame(num));
        }
    }

    private static int killGame(int num) {
        //1个人以下玩不了
        if (num < 2) {
            return -1;
        } else {
            //多少人玩这个游戏
            int peopleNum = num;
            //给玩的人装进数组
            boolean[] arr = new boolean[num];
            //赋值为true
            for (int i = 0; i < arr.length; i++) {
                arr[i] = true;
            }
            //说出的数字
            int numSpeak = 0;
            //活着的人数
            int peopleLife = 0;
            for (int j = 0; j < 1000000; j++) {
                for (int i = 0; i < arr.length; i++) {
                    if (arr[i] == true) {
                        numSpeak++;
                        if (numSpeak == 3) {
                            arr[i] = false;
                            peopleNum--;
                            numSpeak = 0;
                        }
                    }
                    if (peopleNum == 1) {
                        for (int k = 0; k < arr.length; k++) {
                            if (arr[k] == true) {
                                peopleLife = k;
                            }
                        }
                        break;
                    }
                }
                if (peopleNum == 1) {
                    break;
                }
            }
            return peopleLife;
        }

    }

}






